package com.edu.internship.busi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.edu.internship.busi.dto.RoleDTO;
import com.edu.internship.busi.entity.Permission;
import com.edu.internship.busi.entity.Role;
import com.edu.internship.busi.entity.RolePermission;
import com.edu.internship.busi.service.IPermissionService;
import com.edu.internship.busi.service.IRolePermissionService;
import com.edu.internship.busi.service.IRoleService;
import com.edu.internship.util.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.License;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("api/rolePermission")
@Slf4j
@Api(tags = {"角色权限关联的控制器"})
public class RolePermissionController {

    @Resource
    private IRolePermissionService rolePermissionService;
    @Resource
    private IRoleService roleService;
    @Resource
    private IPermissionService permissionService;

    @GetMapping("/getInfo")
    @ApiOperation(value = "根据permission-id获取数据",notes = "根据permission-id获取数据")
    public HashSet<Role> getByPermission(String id){
        LambdaQueryWrapper<RolePermission> rpQw = Wrappers.lambdaQuery();
        rpQw.eq(StringUtils.isNotBlank(id),RolePermission::getPId,id);
        List<RolePermission> rolePermission = rolePermissionService.list(rpQw);
        HashSet<Role> roles = new HashSet<>();
        for (int i = 0; i < rolePermission.size(); i++) {
            Role role = roleService.getById(rolePermission.get(i).getRId());
            if (role != null) {
                roles.add(role);
            }
        }

        return roles;
    }

    @GetMapping("/create")
    @ApiOperation(value = "新增角色-Role",notes = "新增角色-Role")
    public boolean create(String component, String rId){
        log.info("数据:{}",component);
        log.info("数据:{}",rId);
        LambdaQueryWrapper<Permission> pQw = Wrappers.lambdaQuery();
        pQw.eq(StringUtils.isNotBlank(component),Permission::getComponent,component);
        List<String> pId = permissionService.list(pQw).stream().map(Permission::getId).collect(Collectors.toList());
        RolePermission rolePermission = new RolePermission();
        rolePermission.setId(Tools.createRandomCode(5,true));
        rolePermission.setCreatedTime(LocalDateTime.now());
        rolePermission.setPId(pId.get(0));
        rolePermission.setRId(rId);
        rolePermissionService.save(rolePermission);
        return true;
    }

    @GetMapping("/getByRole")
    @ApiOperation(value = "根据role获取permission",notes = "根据role获取permission")
    public List<String> getByRole(String id){
        LambdaQueryWrapper<RolePermission> rpQw = Wrappers.lambdaQuery();
        rpQw.eq(StringUtils.isNotBlank(id),RolePermission::getRId,id);
        List<String> getPId = rolePermissionService.list(rpQw).stream().map(RolePermission::getPId).collect(Collectors.toList());
        LambdaQueryWrapper<Permission> pQw = Wrappers.lambdaQuery();
        pQw.in(Permission::getId,getPId);
        return permissionService.list(pQw).stream().map(Permission::getComponent).collect(Collectors.toList());
    }

    @GetMapping("/getByPermission")
    @ApiOperation(value = "根据Permission获取role",notes = "根据Permission获取role")
    public List<String> getFromPermission(String id){
        LambdaQueryWrapper<RolePermission> rQw = Wrappers.lambdaQuery();
        rQw.eq(StringUtils.isNotBlank(id),RolePermission::getPId,id);
        List<String> getRId = rolePermissionService.list(rQw).stream().map(RolePermission::getRId).collect(Collectors.toList());
        LambdaQueryWrapper<Role> pQw = Wrappers.lambdaQuery();
        pQw.in(Role::getId,getRId);
        log.info("数据:{}",roleService.list(pQw).stream().map(Role::getRoleDesc).collect(Collectors.toList()));
        return roleService.list(pQw).stream().map(Role::getRoleDesc).collect(Collectors.toList());
    }

    @PostMapping("/remove")
    @ApiOperation(value = "删除-RolePermission",notes = "删除-RolePermission")
    public boolean remove(@RequestBody RoleDTO dto) {
        LambdaQueryWrapper<RolePermission> rpQw = Wrappers.lambdaQuery();
        rpQw.eq(StringUtils.isNotBlank(dto.getId()), RolePermission::getRId, dto.getId());
        return rolePermissionService.remove(rpQw);
    }

}
